package cn.skylark.wcm.service.impl;

import cn.skylark.wcm.dao.ResourcesDao;
import cn.skylark.wcm.dao.RoleResourcesDao;
import cn.skylark.wcm.entity.Resources;
import cn.skylark.wcm.service.ResourcesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 权限服务实装serviceImpl层
 *
 * @author chuhl
 * @since
 */
@Service("resourcesService")
public class ResourcesServiceImpl implements ResourcesService {
  @Autowired
  public ResourcesDao resourcesDao;
  @Autowired
  public RoleResourcesDao roleResourcesDao;
  Logger logger = LoggerFactory.getLogger(ResourcesServiceImpl.class);

  /**
   * 分页获取权限
   *
   * @param resources Resources
   * @param start     int
   * @param length    int
   * @return List<Resources>
   */
  @Override
  public PageInfo<Resources> selectByPage( Resources resources, int start, int length ) {
    int page = start / length + 1;
    Example example = new Example(Resources.class);
    // 分页查询
    PageHelper.startPage(page, length);
    List<Resources> userList = resourcesDao.selectByExample(example);
    return new PageInfo<>(userList);
  }

  /**
   * 取得所有权限
   *
   * @return List<Resources>
   */
  @Override
  public List<Resources> queryAll() {
    return resourcesDao.queryAll();
  }

  /**
   * 取得指定范围权限
   *
   * @param map Map<String, Object>
   * @return List<Resources>
   */
  @Override
  @Cacheable(cacheNames = "resources", key = "#map['userid'].toString()+#map['type']")
  public List<Resources> loadUserResources( Map<String, Object> map ) {
    return resourcesDao.loadUserResources(map);
  }

  /**
   * 取得被指定权限
   *
   * @param rid Integer
   * @return List<Resources>
   */
  @Override
  public List<Resources> queryResourcesListWithSelected( Integer rid ) {
    return resourcesDao.queryResourcesListWithSelected(rid);
  }

  /**
   * 保存权限
   *
   * @param resources Resources
   */
  @Override
  public void save( Resources resources ) {
    resources.setDelflg(0);
    resourcesDao.insert(resources);
  }

  /**
   * 通过名字是否重复
   *
   * @param resources 权限的实体类
   * @return
   */
  @Override
  public Resources selectByName( Resources resources ) {
    logger.info("新增时查询权限是否重复开始" + "resources===" + resources);
    Example example = new Example(Resources.class);
    Example.Criteria criteria = example.createCriteria();
    criteria.andEqualTo("name", resources.getName());
    List<Resources> resourcesList = resourcesDao.selectByExample(example);
    logger.info("新增时查询权限是否重复结束");
    if (resourcesList.size() > 0) {
      return resourcesList.get(0);
    }
    return null;
  }

  /**
   * 逻辑删除权限 和权限角色关联表
   *
   * @param userid      用户id
   * @param resourcesid
   */
  @Override
  public void deleteResourceById( Integer userid, String[] resourcesid ) {
    for (int i=0;i<resourcesid.length;i++){
      Date date = new Date();
      //1.修改role_resources中的delflg状态
      Map<String, Object> map = new HashMap<>();
      map.put("updatetime", date);
      map.put("delflg", 1);
      map.put("userId", userid);
      map.put("resourcesid", resourcesid[i]);
      roleResourcesDao.updateById(map);
    }

  }

  /**
   * 通过用户id获取权限列表
   *
   * @param userid 用户id
   * @param start  开始位置
   * @param length 每页多少个数据
   * @return PageInfo<Resources>
   */
  @Override
  public PageInfo<Resources> getResourcesListByUserid( Integer userid, String flag,int start, int length, String searchValue ) {
    logger.info("通过用户id获取权限列表开始===" + "start==" + start + "==length" + length);
    //总共多少页
    int page = start / length + 1;
    // 分页查询
    PageHelper.startPage(page, length);
    Map<String, Object> map = new HashMap<>();
    map.put("userid", userid);
    map.put("name", searchValue);
    List<Resources> resourcesList = resourcesDao.queryAllResourceWithUserid(map);
    logger.info("通过用户id获取权限列表结束");
    return new PageInfo<>(resourcesList);
  }

  /**
   * 删除权限
   *
   * @param id Integer 权限id
   */
  @Override
  @Transactional(propagation = Propagation.REQUIRED, readOnly = false,
          rollbackFor = {Exception.class})
  public void deleteById( Integer id ) {
    resourcesDao.deleteByPrimaryKey(id);
  }

  /**
   * 通过类别获取权限
   * 业务:新建租户，需要为角色分配权限,code为1时,获取租户管理员权限,code为2时,获取租户用户权限
   * @param code
   * @return
   */
  @Override
  public  List<Resources> getResourcesListByParentId(String code){
    logger.info("通过类别获取权限开始 code = "+ code);
    Example example=new Example(Resources.class);
    Example.Criteria criteria = example.createCriteria();
    if("1".equals( code )){
      criteria.andBetween( "sort",1,2 );
    }else{
      criteria.andEqualTo("sort",2);
    }
    List<Resources> resourcesList=resourcesDao.selectByExample(example);
    logger.info("通过类别获取权限结束");
    return resourcesList;
  }
}
